<template>
  <h1>注册页面</h1>
  <el-card style="width: 500px;margin: 0 auto">
    <el-form label-width="100px">
      <el-form-item label="用户名">
        <el-input placeholder="请输入用户名" v-model="user.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input placeholder="请输入密码" type="password" v-model="user.password"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input placeholder="请输入昵称" v-model="user.nickname"></el-input>
      </el-form-item>
      <el-form-item><el-button @click="reg()">注册</el-button></el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>
import {ref} from "vue";
import axios from 'axios';
import qs from 'qs';
import {ElMessage} from 'element-plus'
const user = ref({username:"",password:"",nickname:""})
const reg = ()=>{
  // 将JS对象转成查询字符串格式   qs=query string
  let data = qs.stringify(user.value);
  //通过axios框架发出post请求,将装着用户输入数据的JS对象作为参数传递给服务器
  axios.post('http://localhost:8080/v1/users/reg',data)
  .then((response)=>{
    //response对象代表响应对象,里面的data属性代表服务器响应的数据(JsonResult)
    if (response.data.code==2001){
      ElMessage.success("注册成功!")
    }else {
      ElMessage.error("用户名已存在!")
    }
  })
}
</script>